package dome05.dao;

import dome05.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository( "studentDao05")
public class StudentDaoImpl  implements StudentDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Integer insertStudent(Student student) {
        String sql = "insert into students(number,name,gender,clbum,grade) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql, student.getNumber(), student.getName(), student.getGender(), student.getClbum(), student.getGrade());
        return i;
    }

    @Override
    public Integer updateStudent(Student student, String name) {
        String sql = "update students set number=?,name=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, student.getNumber(), student.getName(), student.getGender(), student.getClbum(), student.getGrade(), name);
        return i;
    }

    @Override
    public Student selectStudent(String number) {
        String sql = "select * from students where number=?";
        Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class), number);
        return student;
    }

    @Override
    public Student FindAllStudent() {
        String sql = "select * from students";
        Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class));
        return student;
    }
}
